package com.ming.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ming.reggie.entity.Category;
import com.ming.reggie.entity.Page;
import com.ming.reggie.exception.BusinessException;
import com.ming.reggie.mapper.CategoryMapper;
import com.ming.reggie.mapper.DishMapper;
import com.ming.reggie.mapper.SetmealMapper;
import com.ming.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired(required = false)
    private CategoryMapper categoryMapper;

    @Autowired(required = false)
    private SetmealMapper setmealMapper;

    @Autowired(required = false)
    private DishMapper dishMapper;

    /**
     * 查询菜品分类数据
     *
     * @param type
     * @return
     */
    @Override
    public List<Category> findAllByType(Integer type) {
        return categoryMapper.findAllByType(type);
    }

    /**
     * 添加菜品
     *
     * @param category
     * @return
     */
    @Override
    public boolean Add(Category category) {
        //判断菜品名是否重复
        Category DbCategory = categoryMapper.findByName(category.getName());
        if(DbCategory != null){
            if(category.getType() == 1) {
                throw new BusinessException("菜品名已存在,请更改");
            }else {
                throw new BusinessException("套餐名已存在,请更改");
            }
        }
        //设置创建和修改时间
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        //插入
        Integer insert = categoryMapper.insert(category);
        return insert>0;

    }


    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Page<Category> findByPage(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);

        List<Category> categoryList = categoryMapper.selectAllOrderBySort();

        PageInfo<Category> pageInfo = new PageInfo<>(categoryList);

        return new Page<Category>(pageInfo.getList(),pageInfo.getTotal(),pageSize,page);
    }

    /**
     * 删除分类
     * @param id
     * @return boolean
     */
    @Override
    public boolean delete(Long id) {
        if(dishMapper.findByCid(id) > 0){
            throw new BusinessException("当前分类存在菜品,请先清空");
        }

        if(setmealMapper.findByCid(id) > 0){
            throw new BusinessException("当前分类存在套餐,请先清空");
        }

        Integer delete = categoryMapper.deleteById(id);

        return delete>0;
    }

    /**
     * 修改分类
     * @param category
     * @return
     */
    @Override
    public boolean update(Category category) {
         if(categoryMapper.findByName2(category) > 0){
             throw new BusinessException("已存在相同分类名称,修改失败");
         }

         category.setUpdateTime(LocalDateTime.now());

         return categoryMapper.update(category)>0;
    }

    /**
     * 查询全部分类
     *
     * @return  List<Category>
     */
    @Override
    public List<Category> findAll() {
        List<Category> categoryList = categoryMapper.findAll();
        return  categoryList;
    }


}
